package com.yanqu.road.dao.impl.beautyescort;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekServerGroup;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortWeekServerGroupDaoImpl extends TempDataObjectDao<EscortWeekServerGroup> {

    public static String getEscortWeekServerGroupSql = "select * from `t_c_escort_week_server_group` where `weekId`=? or `weekId`=?";
    public Map<Integer, Map<Integer, EscortWeekServerGroup>> getEscortWeekServerGroup(int preWeekId, int thisWeekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, EscortWeekServerGroup>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getEscortWeekServerGroupSql);
            int i = 1;
            call.setInt(i++, preWeekId);
            call.setInt(i++, thisWeekId);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortWeekServerGroup data = parse(rs);
                if(!result.containsKey(data.getWeekId())){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                result.get(data.getWeekId()).put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortWeekServerGroupSql = "insert into `t_c_escort_week_server_group`(weekId,serverList,groupId) values (?,?,?)";
    @Override
    protected boolean addData(EscortWeekServerGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getServerList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertEscortWeekServerGroupSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateEscortWeekServerGroupSql = "update `t_c_escort_week_server_group` set serverList=? where weekId=? and groupId=? ";
    @Override
    protected boolean updateData(EscortWeekServerGroup data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getServerList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateEscortWeekServerGroupSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortWeekServerGroup parseData(ResultSet rs) throws SQLException {
        EscortWeekServerGroup escortWeekServerGroup = new EscortWeekServerGroup();
        escortWeekServerGroup.setWeekId(rs.getInt("weekId"));
        escortWeekServerGroup.setServerList(StringUtils.stringToLongList(rs.getString("serverList"), ","));
        escortWeekServerGroup.setGroupId(rs.getInt("groupId"));
        return escortWeekServerGroup;
    }
}
